package com.jingtai.validate.validator;

import com.jingtai.validate.swagger.CApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

public class CApiModelPropertyValidator implements ConstraintValidator<CApiModelProperty, Object> {

	// 默认值_false，用于接收注解上自定义的 required
	private CApiModelProperty defValidate;

	// 1、初始化方法：通过该方法我们可以拿到我们的注解
	@Override
	public void initialize(CApiModelProperty constraintAnnotation) {
		// constraintAnnotation.required() 接收我们自定义的属性，是否为空
		defValidate = constraintAnnotation;
	}

//	String message() default "验证不合法";

	@Override
	public boolean isValid(Object value, ConstraintValidatorContext context) {
		// 是否填值
		if (defValidate.required() && value == null) {
			context.disableDefaultConstraintViolation();
			context.buildConstraintViolationWithTemplate(defValidate.value() + "值不允许为空").addConstraintViolation();
			return false;
		}
		if (defValidate.cNotEmpty()) {
			if (value == null) {
				context.disableDefaultConstraintViolation();
				context.buildConstraintViolationWithTemplate(defValidate.value() + "值不允许为空").addConstraintViolation();
				return false;
			} 
			
//			Assert.notEmpty(null);
			//StringUtils.isNotEmpty(cs)
			
			// String类型的
			if (value instanceof String && StringUtils.isEmpty(value.toString())) {
				context.disableDefaultConstraintViolation();
				context.buildConstraintViolationWithTemplate(defValidate.value() + "不允许为空").addConstraintViolation();
				return false;
			}
			// TODO List类型的
		}

		if (defValidate.cEmail()) {
			if (value == null || StringUtils.isEmpty(value.toString())
					|| !value.toString().matches("^\\w+@(\\w+\\.)+\\w+$")) {
				context.disableDefaultConstraintViolation();
				context.buildConstraintViolationWithTemplate(defValidate.value() + "不合法，请填写合法邮箱")
						.addConstraintViolation();
				return false;
			}
		}
		if (defValidate.cPhone()) {
			if (value == null || StringUtils.isEmpty(value.toString()) || !isPhone(value.toString())) {
				context.disableDefaultConstraintViolation();
				context.buildConstraintViolationWithTemplate(defValidate.value() + "{custom.error}")
						.addConstraintViolation();
				return false;
			}
		}
		if (defValidate.cIntMax() != Integer.MAX_VALUE || defValidate.cIntMin() != Integer.MIN_VALUE) {
			if (value == null || !(value instanceof Integer)) {
				context.disableDefaultConstraintViolation();
				context.buildConstraintViolationWithTemplate(defValidate.value() + "请输入合法的数字").addConstraintViolation();
				return false;
			}
			if (defValidate.cIntMax() != Integer.MAX_VALUE) {
				if (defValidate.cIntMax() < Integer.valueOf(value.toString())) {
					context.disableDefaultConstraintViolation();
					context.buildConstraintViolationWithTemplate(defValidate.value() + "数字超过最大值" + defValidate.cIntMax())
							.addConstraintViolation();
					return false;
				}
			}
			if (defValidate.cIntMin() != Integer.MIN_VALUE) {
				if (defValidate.cIntMin() > Integer.valueOf(value.toString())) {
					context.disableDefaultConstraintViolation();
					context.buildConstraintViolationWithTemplate(defValidate.value() + "数字小于最小值" + defValidate.cIntMin())
							.addConstraintViolation();
					return false;
				}
			}
		}

		if (defValidate.cStrMaxLen() != Integer.MAX_VALUE || defValidate.cStrMinLen() != 0) {
			if (defValidate.cStrMaxLen() != Integer.MAX_VALUE) {
				if (defValidate.cStrMaxLen() < value.toString().length()) {
					context.disableDefaultConstraintViolation();
					context.buildConstraintViolationWithTemplate(defValidate.value() + "长度超过最大值" + defValidate.cIntMax())
							.addConstraintViolation();
					return false;
				}
			}
			if (defValidate.cStrMinLen() != 0) {
				if (defValidate.cStrMinLen() > value.toString().length()) {
					context.disableDefaultConstraintViolation();
					context.buildConstraintViolationWithTemplate(defValidate.value() + "长度小于最小值" + defValidate.cIntMax())
							.addConstraintViolation();
					return false;
				}
			}
		}

		return true;
	}
//	String message() default "验证不合法";

//	/**
//	 * 自定义邮箱验证器
//	 */
//	boolean cemail() default false;
//
//	/**
//	 * 自定义字符串长度最短验证器
//	 */
//	int cStrMinLen() default 0;
//
//	/**
//	 * 自定义字符串长度最长验证器
//	 */
//	int cStrMaxLen() default Integer.MAX_VALUE;
//
//	/**
//	 * 自定义整型最小值验证器
//	 */
//	int cIntMin() default Integer.MIN_VALUE;
//
//	/**
//	 * 自定义整型最大值验证器
//	 */
//	int cIntMax() default Integer.MAX_VALUE;
//
//	/**
//	 * 自定义正则验证器
//	 */
//	String cregexp() default "";
	/**
	 * 验证是否为手机号
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean isPhone(String phone) {
		String pattern = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
		if (Pattern.matches(pattern, phone)) {
			return true;
		} else {
			return false;
		}
	}
}
